﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inspiration.Web.Controllers
{
    public class InspiredController : Controller
    {
        //
        // GET: /Inspired/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string generateButton, string saveButton, FormCollection formValues)
        {
            // Problem:
            // I want a [Generate] and [Save] button on the page.
            // Markup implemented is as such:
            // <input type="submit" value="Generate" /> | <input type="button" value="Save" />
            // Both buttons will route to this method
            // How do I tell which button was clicked.
            if (generateButton != null)
            {
                // Idea: A random name should consist of an adjective followed by a noun
                Inspiration.Web.Models.InspirationEntities entitiesRepository = new Models.InspirationEntities();
                int TotalNounsCount = entitiesRepository.Nouns.Count();
                int TotalAdjectivesCount = entitiesRepository.Adjectives.Count();

                System.Random RNG = new Random((int)System.DateTime.Now.Ticks);
                int adjectiveRandomNumber = RNG.Next(1, TotalAdjectivesCount);
                int nounRandomNumber = RNG.Next(1, TotalNounsCount);

                Inspiration.Web.Models.Adjective adjective = entitiesRepository.Adjectives.Single(adj => adj.AdjectiveId == adjectiveRandomNumber);
                Inspiration.Web.Models.Noun noun = entitiesRepository.Nouns.Single(n => n.NounId == nounRandomNumber);

                ViewData["RandomName"] = String.Format("{0} {1}",
                    adjective.Text.Trim().ToLower(),
                    noun.Text.Trim().ToLower());
            }

            if (saveButton != null)
            {
                formValues.Add("Name", formValues["RandomName"]);
                CodeNamesController controller = new CodeNamesController();
                controller.Create(formValues);
            }

            // how best to return randomResult 
            return View();
        }
    }
}
